--
-- c_detail.lua
--
local onFileDownFinished
local detail22Texture, detail58Texture, detail68Texture, detail63Texture, dirtyTexture, detail04Texture, detail29Texture, detail55Texture, detail35TTexture, brickWallShader, tec
local 	grassShader, roadShader,road2Shader,paveDirtyShader,paveStretchShader,barkShader,rockShader,mudShader,concreteShader,sandShader, bAllValid, bEffectEnabled
local effectParts = {}
----------------------------------------------------------------
-- enableDetail
----------------------------------------------------------------

local function getMakeShader(v)
	--  Create shader with a draw range of 100 units
	local shader,tec = dxCreateShader ( "shaders/detail/fx/detail.fx", 1, 100 )
	if shader then
		dxSetShaderValue( shader, "sDetailTexture", v.texture );
		dxSetShaderValue( shader, "sDetailScale", v.detailScale )
		dxSetShaderValue( shader, "sFadeStart", v.sFadeStart )
		dxSetShaderValue( shader, "sFadeEnd", v.sFadeEnd )
		dxSetShaderValue( shader, "sStrength", v.sStrength )
		dxSetShaderValue( shader, "sAnisotropy", v.sAnisotropy )
	end
	return shader,tec
end

----------------------------------------------------------------
-- All the shaders
----------------------------------------------------------------
-- brick wall type thing
---------------------------------
local function getBrickWallSettings ()
	local v = {}
	v.texture=detail22Texture
	v.detailScale=5
	v.sFadeStart=60
	v.sFadeEnd=100
	v.sStrength=0.6
	v.sAnisotropy=1
	return v
end
---------------------------------

-- grass
---------------------------------
local function getGrassSettings ()
	local v = {}
	v.texture=detail58Texture
	v.detailScale=3
	v.sFadeStart=60
	v.sFadeEnd=100
	v.sStrength=0.6
	v.sAnisotropy=1
	return v
end
---------------------------------

-- road
---------------------------------
local function getRoadSettings ()
	local v = {}
	v.texture=detail68Texture
	v.detailScale=1
	v.sFadeStart=60
	v.sFadeEnd=100
	v.sStrength=0.6
	v.sAnisotropy=1
	return v
end
---------------------------------

-- road2
---------------------------------
local function getRoad2Settings ()
	local v = {}
	v.texture=detail63Texture
	v.detailScale=1
	v.sFadeStart=90
	v.sFadeEnd=100
	v.sStrength=0.7
	v.sAnisotropy=0.9
	return v
end
---------------------------------

-- dirty pave
---------------------------------
local function getPaveDirtySettings ()
	local v = {}
	v.texture=dirtyTexture
	v.detailScale=2
	v.sFadeStart=60
	v.sFadeEnd=100
	v.sStrength=0.4
	v.sAnisotropy=1
	return v
end
---------------------------------

-- stretch pave 
---------------------------------
local function getPaveStretchSettings ()
	local v = {}
	v.texture=detail04Texture
	v.detailScale=2
	v.sFadeStart=80
	v.sFadeEnd=100
	v.sStrength=0.3
	v.sAnisotropy=1
	return v
end
---------------------------------

-- tree bark
---------------------------------
local function getBarkSettings ()
	local v = {}
	v.texture=detail29Texture
	v.detailScale=2
	v.sFadeStart=80
	v.sFadeEnd=100
	v.sStrength=0.6
	v.sAnisotropy=1
	return v
end
---------------------------------

-- rock
---------------------------------
local function getRockSettings ()
	local v = {}
	v.texture=detail55Texture
	v.detailScale=2
	v.sFadeStart=80
	v.sFadeEnd=100
	v.sStrength=0.5
	v.sAnisotropy=1
	return v
end
---------------------------------

-- mud
---------------------------------
local function getMudSettings ()
	local v = {}
	v.texture=detail35TTexture
	v.detailScale=3
	v.sFadeStart=80
	v.sFadeEnd=100
	v.sStrength=0.6
	v.sAnisotropy=1
	return v
end
---------------------------------

local function getBrickWallShader()
	return getMakeShader( getBrickWallSettings () )
end

local function getGrassShader()
	return getMakeShader( getGrassSettings () )
end

local function getRoadShader()
	return getMakeShader( getRoadSettings () )
end

local function getRoad2Shader()
	return getMakeShader( getRoad2Settings () )
end

local function getPaveDirtyShader()
	return getMakeShader( getPaveDirtySettings () )
end

local function getPaveStretchShader()
	return getMakeShader( getPaveStretchSettings () )
end

local function getBarkShader()
	return getMakeShader( getBarkSettings () )
end

local function getRockShader()
	return getMakeShader( getRockSettings () )
end

local function getMudShader()
	return getMakeShader( getMudSettings () )
end


local function disableDetail()
	if not bEffectEnabled then return end

	-- Destroy all parts
	for _,part in ipairs(effectParts) do
		if part then
			destroyElement( part )
		end
	end
	effectParts = {}
	bAllValid = false

	-- Flag effect as stopped
	bEffectEnabled = false
end

	local downloading, dTimer = false, false
	




local function enableDetail()
	if bEffectEnabled then return end
	local _return = false
		for k, v in pairs( { 'detail22.png', 'detail58.png' ,'detail68.png', 'detail63.png', "detail04.png", "detail29.png", "detail35T.png", "detail55.png", 'dirty.png' } ) do 
			if not fileExists( 'shaders/detail/media/'..v ) then
				if not downloading then
					--initiateFileTransfer( 'shaders/detail/media/'..v, 250000 )
					downloadFile( 'shaders/detail/media/'..v )
				end	
				_return = true
			end	
		end	
		
		if _return then
			downloading = true
			addEventHandler ( "onClientFileDownloadComplete", getRootElement(), onFileDownFinished )
			return 0;
		end	
	
	-- Load textures
	detail22Texture = dxCreateTexture('shaders/detail/media/detail22.png', "dxt3")
	detail58Texture = dxCreateTexture('shaders/detail/media/detail58.png', "dxt3")
	detail68Texture = dxCreateTexture('shaders/detail/media/detail68.png', "dxt1")
	detail63Texture = dxCreateTexture('shaders/detail/media/detail63.png', "dxt3")
	dirtyTexture = dxCreateTexture('shaders/detail/media/dirty.png', "dxt3")
	detail04Texture = dxCreateTexture('shaders/detail/media/detail04.png', "dxt3")
	detail29Texture = dxCreateTexture('shaders/detail/media/detail29.png', "dxt3")
	detail55Texture = dxCreateTexture('shaders/detail/media/detail55.png', "dxt3")
	detail35TTexture = dxCreateTexture('shaders/detail/media/detail35T.png', "dxt3")

	-- Create shaders
	brickWallShader, tec = getBrickWallShader()
	if brickWallShader then
		-- Only create the rest if the first one is OK
		grassShader = getGrassShader()
		roadShader = getRoadShader()
		road2Shader = getRoad2Shader()
		paveDirtyShader = getPaveDirtyShader()
		paveStretchShader = getPaveStretchShader()
		barkShader = getBarkShader()
		rockShader = getRockShader()
		mudShader = getMudShader()
		concreteShader = getBrickWallShader()	-- TODO make this better
		sandShader = getMudShader()				-- TODO make this better
	end

	-- Get list of all elements used
	effectParts = {
						detail22Texture, detail58Texture, detail68Texture, detail63Texture, dirtyTexture,
						detail04Texture, detail29Texture, detail55Texture, detail35TTexture,
						brickWallShader, grassShader, roadShader, road2Shader, paveDirtyShader,
						paveStretchShader, barkShader, rockShader, mudShader,
						concreteShader, sandShader
					}

	-- Check list of all elements used
	bAllValid = true
	for _,part in ipairs(effectParts) do
		bAllValid = part and bAllValid
	end

	bEffectEnabled = true

	if not bAllValid then
	
		disableDetail()
	else


		engineApplyShaderToWorldTexture ( roadShader, "*road*" )
		engineApplyShaderToWorldTexture ( roadShader, "*tar*" )
		engineApplyShaderToWorldTexture ( roadShader, "*asphalt*" )
		engineApplyShaderToWorldTexture ( roadShader, "*freeway*" )
		engineApplyShaderToWorldTexture ( concreteShader, "*wall*" )
		engineApplyShaderToWorldTexture ( concreteShader, "*floor*" )
		engineApplyShaderToWorldTexture ( concreteShader, "*bridge*" )
		engineApplyShaderToWorldTexture ( concreteShader, "*conc*" )
		engineApplyShaderToWorldTexture ( concreteShader, "*drain*" )
		engineApplyShaderToWorldTexture ( concreteShader, "*fence*" )
		engineApplyShaderToWorldTexture ( concreteShader, "*brick*" )
		engineApplyShaderToWorldTexture ( barkShader, "*tree*" )
		engineApplyShaderToWorldTexture ( barkShader, "*lamp*" )


		engineApplyShaderToWorldTexture ( paveDirtyShader, "*walk*" )
		engineApplyShaderToWorldTexture ( paveDirtyShader, "*pave*" )
		engineApplyShaderToWorldTexture ( paveDirtyShader, "tenniscourt1_256" )
		engineApplyShaderToWorldTexture ( paveDirtyShader, "*cross*" )

		engineApplyShaderToWorldTexture ( mudShader, "*mud*" )
		engineApplyShaderToWorldTexture ( mudShader, "*dirt*" )
		engineApplyShaderToWorldTexture ( rockShader, "*rock*" )
		engineApplyShaderToWorldTexture ( rockShader, "*stone*" )
		engineApplyShaderToWorldTexture ( grassShader, "*grass*" )
		engineApplyShaderToWorldTexture ( grassShader, "*path*" )
		engineApplyShaderToWorldTexture ( grassShader, "*hedge*" )
		engineApplyShaderToWorldTexture ( grassShader, "desertgryard256" )	-- grass
		engineApplyShaderToWorldTexture ( grassShader, "sw_crops" )	-- grass

		engineApplyShaderToWorldTexture ( sandShader, "*sand*" )
		engineApplyShaderToWorldTexture ( barkShader, "*leave*" )
		engineApplyShaderToWorldTexture ( barkShader, "*log*" )
		engineApplyShaderToWorldTexture ( barkShader, "*bark*" )

		-- Roads
		engineApplyShaderToWorldTexture ( roadShader, "*carpark*" )
		engineApplyShaderToWorldTexture ( road2Shader, "*hiway*" )
		engineApplyShaderToWorldTexture ( roadShader, "*junction*" )
		engineApplyShaderToWorldTexture ( paveStretchShader, "snpedtest*" )

		-- Pavement
		engineApplyShaderToWorldTexture ( paveStretchShader, "sidelatino*" )
		engineApplyShaderToWorldTexture ( paveStretchShader, "sjmhoodlawn41" )

		-- Remove detail from LOD models etc.
		for i,part in ipairs(effectParts) do
			if getElementType(part) == "shader" then
				engineRemoveShaderFromWorldTexture ( part, "tx*" )
				engineRemoveShaderFromWorldTexture ( part, "lod*" )
			end
		end
	end

end


local function onFileDownFinished_( file, success )

	if string.find( file, 'shaders/detail/media/' ) and not success then

			downloadFile( file )

	end


		for k, v in pairs( { 'detail22.png', 'detail58.png' ,'detail68.png', 'detail63.png', "detail04.png", "detail29.png", "detail35T.png", "detail55.png", 'dirty.png' } ) do 
			if not fileExists( 'shaders/detail/media/'..v ) then
				return false
			end
		end		
		removeEventHandler ( "onClientFileDownloadComplete", getRootElement(), onFileDownFinished )
		setTimer( enableDetail, 500, 1 )
		downloading = false
end	




----------------------------------------------------------------
-- disableDetail
----------------------------------------------------------------
onFileDownFinished = onFileDownFinished_




function handleOnClientSwitchDetail( bOn )
	
	if bOn then
		enableDetail()
	else
		disableDetail()
	end
end

